package com.hxm.appGenealogy.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxm.appGenealogy.Function.FamilyFunction;
import com.hxm.appGenealogy.Function.PeopleFunction;
import com.hxm.appGenealogy.config.ParmIntended;
import com.hxm.appGenealogy.config.queryVo.QueryWrapperVO;
import com.hxm.appGenealogy.entity.*;
import com.hxm.appGenealogy.entryVo.PhotoDto;
import com.hxm.appGenealogy.mapper.PhtotoMapper;
import com.hxm.appGenealogy.mapper.imagePhMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("app/imageph")
public class ImagePhController {
    @Autowired
    private imagePhMapper imageMapper;

    @Autowired
    private PeopleFunction peopleFunction;

    @Autowired
    private PhtotoMapper phtotoMapper;

    @Autowired
    private FamilyFunction familyFunction;


    /**
     * 创建相册;
     * @param
     * @return
     */
    @PostMapping("add")
    public ResponDataVo add(@RequestBody imagePh imagePh) {
        String parmAnalysis = ParmIntended.parmAnalysis(imagePh,"peopleId");
        if(parmAnalysis!=null){
            return new ResponDataVo(parmAnalysis);
        }
        //设置创建的时间;
        imagePh.setTime(new Date());
        Long peopleId = imagePh.getPeopleId();
        People people = peopleFunction.peopleGetByid(peopleId);
        if(people==null){
            return new ResponDataVo("没有当前的账号的信息;");
        }
        //设置所属于的家族;
        imagePh.setFanilyId(people.getFamilyId());
        imagePh.setQuantity(0);
        imageMapper.insert(imagePh);
        return new ResponDataVo(true);
    }
     /*
     * @param imagevo
     * @return
     * id 当前相册的id;
     */
    @PostMapping("selectPhotoImage")
    public ResponDataVo selectPhotoImage(@RequestBody imagePhVo imagevo) {
        String parmAnalysis = ParmIntended.parmAnalysis(imagevo, "id", "current", "size");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        Page page = new Page(imagevo.getCurrent(),imagevo.getSize());
        //获取当前的相册的信息;
        Page selectPage = phtotoMapper.selectPage(page, new QueryWrapper<Photo>().eq("iamge_ph", imagevo.getId()));
        Map<String,Object> stringObjectMap = new HashMap<>();
        List records = selectPage.getRecords();
        stringObjectMap.put("records",records);
        stringObjectMap.put("total",selectPage.getTotal());
        System.out.println(JSON.toJSONString(stringObjectMap));
        return new ResponDataVo(stringObjectMap);
    }
    /**
     * 往相册中添加照片;
     */
    @PostMapping("addImage")
    public ResponDataVo addImage(@RequestBody Photo photo) {
        String parmAnalysis = ParmIntended.parmAnalysis(photo, "iamge", "imagePhId", "size");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        //获取家族的相册的数据;
        imagePh imagePh = imageMapper.selectById(photo.getImagePhId());
        //判断当前家族的存储的空间的大小;
        Family family = (Family) familyFunction.selectGetOneQuery(JSON.toJSONString(new QueryWrapperVO()
                .eq("id", imagePh.getFanilyId())
        )).getData();
        if (family == null) {
            return new ResponDataVo("请先创建家族;");
        }
        if (family.getUsableSpace() - family.getUseSpace() <= photo.getSize()) {
            return new ResponDataVo("您的存储空间不足");
        }
        //增加已经使用的相册空间的大小;
        family.setUseSpace(family.getUseSpace() + photo.getSize());
        familyFunction.updateById(family);
        photo.setTime(new Date());
        //添加空间;
        phtotoMapper.insert(photo);
        //相册的照片的数量+1;
        imagePh.setQuantity(imagePh.getQuantity() + 1);
        return null;
    }

    /**
     * 批量往相册中添加照片;
     * @param photoDto
     * @return
     */
    @PostMapping("addListImage")
    public ResponDataVo addListImage(@RequestBody PhotoDto photoDto) {
        String parmAnalysis = ParmIntended.parmAnalysis(photoDto, "imagePhId");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        List<Photo> photoList = photoDto.getPhotoList();
        if (photoList.size() <= 0) {
            return new ResponDataVo("缺少照片数据;");
        }
        //获取当前相册的信息;
        imagePh imagePh = imageMapper.selectById(photoDto.getImagePhId());
        //获取当前的家族的信息;
        Family family = (Family)familyFunction.selectByid(imagePh.getFanilyId()).getData();
        Double imageSize = 0.00;
        //循环添加照片的数据;
        for (Photo photo : photoList) {
            Double size = photo.getSize();
            imageSize+=size;
        }
        if(family.getUsableSpace()+family.getUseSpace()<=imageSize){
            return new ResponDataVo("您的剩余空间不足;");
        }
        //添加照片的数据;
        photoList.forEach(f->{
            f.setTime(new Date());
            f.setImagePhId(photoDto.getImagePhId());
            phtotoMapper.insert(f);
        });
        //增加相册的存储量;
        imagePh.setQuantity(imagePh.getQuantity() + photoList.size());
        imageMapper.updateById(imagePh);
        //增加空间的存储量;
        family.setUseSpace(family.getUseSpace()+imageSize);
        familyFunction.updateById(family);
        return new ResponDataVo(true);
    }

    /**
     * 修改相册的信息;
     */
    @PostMapping("updateByImageId")
    private ResponDataVo updateImagePhBuId(@RequestBody imagePh imagePh){
        String parmAnalysis = ParmIntended.parmAnalysis(imagePh, "id");
        if(parmAnalysis!=null){
            return new ResponDataVo(parmAnalysis);
        }
        imagePh imagePh1 = imageMapper.selectById(imagePh.getId());
        if(imagePh1.getPhotoName().equals("默认相册")){
            return new ResponDataVo("默认相册不可以修改;");
        }
        imageMapper.updateById(imagePh);
        return new ResponDataVo(true);
    }


    /**
     * 查询相册;
     */
    @PostMapping("selImg")
    private ResponDataVo selectImagePh(@RequestBody Family family){
        List<imagePh> imagePhs = imageMapper.selectList(new QueryWrapper<imagePh>()
                .eq("fanily_id", family.getId()));
        return new ResponDataVo(imagePhs);
    }

    /**
     * 删除相册;
     */
    @PostMapping("delimageph")
    public ResponDataVo delImage(@RequestBody imagePh imagePh){
        String parmAnalysis = ParmIntended.parmAnalysis(imagePh, "id");
        if(parmAnalysis!=null){
            return new ResponDataVo(parmAnalysis);
        }
        //判断当前的相册当中是否还存在照片的数据;
        imagePh imagePhSelect = imageMapper.selectById(imagePh.getId());
        if(imagePhSelect==null){
            return new ResponDataVo("没有当前的相册的信息;");
        }
        if(imagePhSelect.getPhotoName().equals("默认相册")){
            return new ResponDataVo("默认相册不可以删除;");
        }
        //判断当前的相册当中是否有照片;
        List<Photo> iamge_ph = phtotoMapper.selectList(new QueryWrapper<Photo>().eq("iamge_ph", imagePhSelect.getId()));
        if(iamge_ph.size()>0){
            return new ResponDataVo("请先删除相册中的照片之后在删除相册;");
        }
        imageMapper.deleteById(imagePhSelect.getId());
        return new ResponDataVo(true);
    }





}









